<template>
	<view class="page-body">
		<!-- 页面内容开始 -->

		<!-- 表格搜索组件开始 -->
		<vk-data-table-query
			v-model="queryForm1.formData"
			:columns="queryForm1.columns"
			@search="search"
		></vk-data-table-query>
		<!-- 表格搜索组件结束 -->

		<!-- 自定义按钮区域开始 -->
		<view>
			<el-row>
				<el-button type="success" size="small" icon="el-icon-circle-plus-outline" @click="addBtn">添加</el-button>
				<!-- 批量操作 -->
				<el-dropdown v-if="table1.multipleSelection" :split-button="false"	trigger="click" @command="batchBtn">
					<el-button type="danger" size="small" style="margin-left: 20rpx;"
						:disabled="table1.multipleSelection.length === 0"
					>
						批量操作<i class="el-icon-arrow-down el-icon--right"></i>
					</el-button>
					<el-dropdown-menu slot="dropdown">
						<el-dropdown-item :command="1">批量删除</el-dropdown-item>
						<el-dropdown-item :command="2">批量审核</el-dropdown-item>
					</el-dropdown-menu>
				</el-dropdown>
				<!-- <el-button type="success" size="small" icon="el-icon-edit-outline" style="margin-left: 20rpx;" @click="exportExcel" > 订单导出xls </el-button> -->
				<el-button type="success" size="small" icon="el-icon-edit-outline" style="margin-left: 20rpx;" @click="exportExcelAll" > 导出全部 </el-button>
			</el-row>
		</view>
		<!-- 自定义按钮区域结束 -->

		<!-- 表格组件开始 -->
		<vk-data-table
			ref="table1"
			:action="table1.action"
			:columns="table1.columns"
			:query-form-param="queryForm1"
			:right-btns="['detail_auto','update','delete','more']"
			:right-btns-more="table1.rightBtnsMore"
			:selection="true"
			:row-no="true"
			:pagination="true"
			@update="updateBtn"
			@delete="deleteBtn"
			@current-change="currentChange"
			@selection-change="selectionChange"
		></vk-data-table>
		<!-- 表格组件结束 -->

		<!-- 添加或编辑的弹窗开始 -->
		<vk-data-dialog
			v-model="form1.props.show"
			:title="form1.props.title"
			width="500px"
			mode="form"
			:close-on-click-modal="false"
			:destroy-on-close="true"
		>
			<vk-data-form
				v-model="form1.data"
				:rules="form1.props.rules"
				:action="form1.props.action"
				:form-type="form1.props.formType"
				:columns='form1.props.columns'
				label-width="80px"
				@success="form1.props.show = false;refresh();"
			></vk-data-form>
		</vk-data-dialog>
		<!-- 添加或编辑的弹窗结束 -->

		<!-- 页面内容结束 -->
	</view>
</template>

<script>
	var that;													// 当前页面对象
	var vk = uni.vk;									// vk实例
	var originalForms = {};						// 表单初始化数据

	export default {
		data() {
			// 页面数据变量
			return {
				// 页面是否请求中或加载中
				loading:false,
				// init请求返回的数据
				data:{

				},
				// 表格相关开始 -----------------------------------------------------------
				table1:{
					// 表格数据请求地址
					action:"admin/hymbjyxh/nrgl/kh/nrgl-obj.getTableList",
					// 表格字段显示规则
					columns:[
						{ key: "title", title: "标题", type:"text", width:220 },
						{ key: "zuozhe", title: "发布者", type: "text",width:180 },
						{ key: "image1", title: "封面", type: "image", width: 120, imageWidth:60 },
						{
						  key: "nrtype", title: "新闻类型", type: "checkbox",width:220,
						  data: [
						    { value: 'xwzx', label: "新闻资讯" },
						    { value: 'zcfg', label: "政策法规" },
							{ value: 'gjzcfg', label: "国家政策法规" },
							{ value: 'bszcfg', label: "本省政策法规" },
							{ value: 'wszcfg', label: "外省政策法规" },
							{ value: 'szzcfg', label: "市州政策法规" },
							{ value: 'llyj', label: "理论研究" },
							{ value: 'xhwj', label: "协会文件" },
							{ value: 'xhhd', label: "协会活动" },
							{ value: 'xhhd-lsnh', label: "理事年会" },
							{ value: 'xhhd-dyhd', label: "调研活动" },
							{ value: 'xhhd-sjjl', label: "省际交流" },
							{ value: 'xhhd-jxtj', label: "典型推介" },
							{ value: 'xhhd-xhjy', label: "协会经验" },
							{ value: 'djgz', label: "党建工作" },
							{ value: 'djgztzgg', label: "通知公告" },
							{ value: 'djgz-gzdt', label: "工作动态" },
							{ value: 'djgz-jyts', label: "经验特色" },
							{ value: 'sytw', label: "首页图文" },
							{ value: 'xhgk', label: "协会概况" },
							{ value: 'mxrw', label: "民校人物" },
							{ value: 'hyfc', label: "会员风采" },
							{ value: 'zlxz', label: "资料下载" },
							{ value: 'yqlj', label: "友情链接" },
						  ]
						},
						{
						  key: "radio1", title: "审核状态", type: "radio", width:100,
						  data: [
							{ value: '1', label: "审核通过" },
						    { value: '2', label: "待审核" },
						    { value: '0', label: "未审核通过" }
						  ]
						},
						{ key: "editor", title: "富文本", type: "html", show:["detail"] },
						{ key: "_add_time", title: "时间", type: "time", width: 160, valueFormat:"yyyy-MM-dd hh:mm:ss" },
						{ key: "_id", title: "id", type:"text", width:220 },
					],
					// 多选框选中的值
					multipleSelection:[],
					// 当前高亮的记录
					selectItem:"",
					rightBtnsMore:[
						{
							title:'编辑器',
							onClick:function(item){
								vk.setStorageSync("hdfb_key",item);
								vk.navigateTo("/pages/nrgl/nrgl-table-editor");
							}
						},
						{
							title:'按钮2',
							onClick:function(item){
								vk.toast(`${item._id}-按钮2`);
							}
						}
					]
				},
				// 表格相关结束 -----------------------------------------------------------
				// 表单相关开始 -----------------------------------------------------------
				// 查询表单请求数据
				queryForm1:{
					// 查询表单数据源，可在此设置默认值
					formData:{

					},
					// 查询表单的字段规则 fieldName:指定数据库字段名,不填默认等于key
					columns:[
						// { key:"money_1", type:"money", title:"金额范围", width:160, placeholder:"请输入最小金额",
						// 	mode:">=", fieldName:"money"
						// },
						// { key:"money_2", type:"money", title:"-", width:160, placeholder:"请输入最大金额",
						//  mode:"<=" , fieldName:"money"
						// },
						{ key:"title", title:"标题", type:"text", width:160, mode:"%%" },
						{ key:"zuozhe", title:"作者", type:"text", width:160, mode:"%%" },
						{
						  key: "radio1", title: "审核状态", type: "radio", optionType: "button",
						  data: [
							{ value: '1', label: "审核通过" },
						    { value: '2', label: "待审核" },
						    { value: '0', label: "未审核通过" }
						  ]
						},
						{
						  key: "nrtype", title: "新闻类型", type: "radio",itemWidth: 180,
						  data: [
						    { value: 'xwzx', label: "新闻资讯" },
						    { value: 'zcfg', label: "政策法规" },
							{ value: 'gjzcfg', label: "国家政策法规" },
							{ value: 'bszcfg', label: "本省政策法规" },
							{ value: 'wszcfg', label: "外省政策法规" },
							{ value: 'szzcfg', label: "市州政策法规" },
							{ value: 'llyj', label: "理论研究" },
							{ value: 'xhwj', label: "协会文件" },
							{ value: 'xhhd', label: "协会活动" },
							{ value: 'xhhd-lsnh', label: "理事年会" },
							{ value: 'xhhd-dyhd', label: "调研活动" },
							{ value: 'xhhd-sjjl', label: "省际交流" },
							{ value: 'xhhd-jxtj', label: "典型推介" },
							{ value: 'xhhd-xhjy', label: "协会经验" },
							{ value: 'djgz', label: "党建工作" },
							{ value: 'djgztzgg', label: "通知公告" },
							{ value: 'djgz-gzdt', label: "工作动态" },
							{ value: 'djgz-jyts', label: "经验特色" },
							{ value: 'sytw', label: "首页图文" },
							{ value: 'xhgk', label: "协会概况" },
							{ value: 'mxrw', label: "民校人物" },
							{ value: 'hyfc', label: "会员风采" },
							{ value: 'zlxz', label: "资料下载" },
							{ value: 'yqlj', label: "友情链接" },
						  ]
						},
						{ key:"_add_time", title:"时间", type:"datetimerange", width:400, mode:"[]" },
					]
				},
				form1:{
					// 表单请求数据，此处可以设置默认值
					data: {
						"radio1":"2"
					},
					// 表单属性
					props: {
						// 表单请求地址
						action:"",
						// 表单字段显示规则
						columns:[
							{ key: "title", title: "标题", type: "text" },
							{ key: "image1", title: "封面", type: "image", limit: 1, cloudPathRemoveChinese: true },
							{
							  key: "nrtype", title: "新闻类型", type: "checkbox",
							  itemWidth: 80,
							  data: [
							    { value: 'xwzx', label: "新闻资讯" },
							    { value: 'zcfg', label: "政策法规" },
								{ value: 'gjzcfg', label: "国家政策法规" },
								{ value: 'bszcfg', label: "本省政策法规" },
								{ value: 'wszcfg', label: "外省政策法规" },
								{ value: 'szzcfg', label: "市州政策法规" },
								{ value: 'xhhd', label: "协会活动" },
								{ value: 'xhhd-lsnh', label: "理事年会" },
								{ value: 'xhhd-dyhd', label: "调研活动" },
								{ value: 'xhhd-sjjl', label: "省际交流" },
								{ value: 'xhhd-jxtj', label: "典型推介" },
								{ value: 'xhhd-xhjy', label: "协会经验" },
								{ value: 'llyj', label: "理论研究" },
							    { value: 'xhwj', label: "协会文件" },
								{ value: 'mjrw', label: "民校任务" },
								{ value: 'djgz', label: "党建工作" },
								{ value: 'djgztzgg', label: "通知公告" },
								{ value: 'djgz-gzdt', label: "工作动态" },
								{ value: 'djgz-jyts', label: "经验特色" },
								{ value: 'sytw', label: "首页图文" },
								{ value: 'xhgk', label: "协会概况" },
								{ value: 'mxrw', label: "民校人物" },
								{ value: 'hyfc', label: "会员风采" },
								{ value: 'zlxz', label: "资料下载" },
								{ value: 'yqlj', label: "友情链接" },
							  ]
							},
							{
							  key: "radio1", title: "审核状态", type: "radio", itemWidth: 80,
							  data: [
								{ value: '1', label: "审核通过" },
							    { value: '2', label: "待审核" },
							    { value: '0', label: "未审核通过" }
							  ]
							},
							{ key:"editor", title:"多功能富文本组件", type:"custom", component:"custom-editor-tinymce", width: 750, height: 500, placeholder: "开始输入..." },
							{ key: "zuozhe", title: "发布者", type: "text" },
							{ key: "linkdizhi", title: "链接", type: "text" },
							{ key: "_add_time", title: "时间", type: "date", valueFormat: "timestamp", dateType: "datetime", tips: "可选择年月日时分秒" },
						],
						// 表单验证规则
						rules:{
							title:[
								{ required:true, message: "不能为空", trigger:"change" }
							],
							nrtype:[
								{ required:true, message: "不能为空", trigger:"change" }
							],
							radio1:[
								{ required:true, message: "不能为空", trigger:"change" }
							],
							editor:[
								{ required:true, message: "不能为空", trigger:"change" }
							],
							zuozhe:[
								{ required:true, message: "不能为空", trigger:"change" }
							],
							nrdate:[
								{ required:true, message: "不能为空", trigger:"change" }
							],
							money:[
								{ required:true, message:"金额不能为空", trigger:"blur" },
								{ type:"number", message:"金额必须是数字", trigger:"blur" },
							],
						},
						// add 代表添加 update 代表修改
						formType:"",
						// 是否显示表单的弹窗
						show:false
					}
				},
				formDatas:{}
				// 表单相关结束 -----------------------------------------------------------
			};
		},
		// 监听 - 页面每次【加载时】执行(如：前进)
		onLoad(options = {}) {
			that = this;
			vk = that.vk;
			that.options = options;
			that.init(options);
		},
		// 监听 - 页面【首次渲染完成时】执行。注意如果渲染速度快，会在页面进入动画完成前触发
		onReady() {},
		// 监听 - 页面每次【显示时】执行(如：前进和返回) (页面每次出现在屏幕上都触发，包括从下级页面点返回露出当前页面)
		onShow() {},
		// 监听 - 页面每次【隐藏时】执行(如：返回)
		onHide() {},
		// 函数
		methods: {
			// 页面数据初始化函数
			init(options) {
				originalForms["form1"] = vk.pubfn.copyObject(that.form1);
			},
			// 页面跳转
			pageTo(path) {
				vk.navigateTo(path);
			},
			// 页面返回
			pageBack(path){
				const pages = getCurrentPages();
				if (pages.length>1 && pages[0].route !== that.$page.route) {
					vk.navigateBack();
				} else if(path) {
					vk.navigateTo(path);
				}
			},
			// 表单重置
			resetForm(){
				vk.pubfn.resetForm(originalForms, that);
			},
			// 搜索
			search(){
				that.$refs.table1.search();
			},
			// 刷新
			refresh(){
				that.$refs.table1.refresh();
			},
			// 获取当前选中的行的数据
			getCurrentRow(){
				return that.$refs.table1.getCurrentRow();
			},
			// 监听 - 行的选中高亮事件
			currentChange(val){
				that.table1.selectItem = val;
			},
			// 当选择项发生变化时会触发该事件
			selectionChange(list) {
				that.table1.multipleSelection = list;
			},
			// 显示添加页面
			addBtn(){
				that.resetForm();
				that.form1.props.action = 'admin/hymbjyxh/nrgl/kh/nrgl-obj.addObjOne';
				that.form1.props.formType = 'add';
				that.form1.props.title = '添加';
				that.form1.props.show = true;
			},
			// 显示修改页面
			updateBtn({ item }){
				that.form1.props.action = 'admin/hymbjyxh/nrgl/kh/nrgl-obj.updateObjOne';
				that.form1.props.formType = 'update';
				that.form1.props.title = '编辑';
				that.form1.props.show = true;
				that.form1.data = item;
			},
			// 删除按钮
			deleteBtn({ item, deleteFn }){
				deleteFn({
					action: "admin/hymbjyxh/nrgl/kh/nrgl-obj.deleteObjOne",
					data:{
						_id: item._id
					},
					refresh: true
				});
			},
			async plscrwxx(){
				//收集ID 批量审核操作操作
				let _ids = [];
				that.table1.multipleSelection.map((item,index)=>{
						_ids.push(item._id);
				});
				await vk.callFunction({
				  url: 'admin/hymbjyxh/nrgl/kh/nrgl-obj.deleteList',
				  title:'请求中...',
				  data:{
				    _ids:_ids
				  }
				}).then((data) => {
					 // 刷新操作
					 that.$notify({
						message:'删除了:'+data.num+'条数据',
						type:'success'
					 });
					  that.$refs.table1.refresh();

				}).catch((err) => {
				  
				});
			},
			async updateListSh(){
				//收集ID 批量审核操作操作
				let _ids = [];
				that.table1.multipleSelection.map((item,index)=>{
						_ids.push(item._id);
				});
				await vk.callFunction({
				  url: 'admin/hymbjyxh/nrgl/kh/nrgl-obj.updateList',
				  title:'请求中...',
				  data:{
				    _ids:_ids
				  }
				}).then((data) => {
					 // 刷新操作
					 that.$notify({
						message:'删除了:'+data.num+'条数据',
						type:'success'
					 });
					  that.$refs.table1.refresh();

				}).catch((err) => {
				  
				});
			},
			// 监听 - 批量操作的按钮点击事件 updateListSh
			batchBtn(index){
				switch(index){
					case 1: that.plscrwxx(); break;
					case 2: that.updateListSh(); break;
					default : break;
				}
			},
			// 导出xls表格文件（表格当前页数据）
			exportExcel(){
				that.$refs.table1.exportExcel();
			},
			// 导出xls表格文件（全部数据）
			exportExcelAll(){
				that.$refs.table1.exportExcel({
					fileName: "表格全部数据",
					title: "正在导出数据...",
					pageIndex: 1,
					pageSize: -1, // 此值为-1，代表导出所有数据
				});
			}
		},
		// 监听属性
		watch: {

		},
		// 过滤器
		filters: {

		},
		// 计算属性
		computed: {

		}
	};
</script>
<style lang="scss" scoped>
	.page-body {

	}
</style>
